<template>
    <div class="login">
        <div class="login_form">
            <div class="title">登录小工单</div>
            <div class="login_way clearfix">
                <span @click='changeLoginWay(0)' :class="currentSpan == 0 ? 'active' : ''">手机号登陆</span>
                <span @click="changeLoginWay(1)" :class="currentSpan == 1 ? 'active' : ''">工厂账号登录</span>
            </div>
            <el-form :model="form" size="mini" ref="form" v-loading="loading" element-loading-text="加载中"
                element-loading-background="rgba(250, 250, 250, 0.5)" element-loading-spinner="el-icon-loading"
                :rules="rules">
                <el-form-item prop="factoryCode" v-if="currentSpan == 1">
                    <el-input v-model="form.factoryCode" placeholder="工厂代码" maxlength="6">
                        <template slot="prepend">
                            <span class="el-icon-office-building"></span>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="account">
                    <el-input v-model="form.account" :placeholder="currentSpan == 0 ? '手机号' : '账号'" maxlength="11">
                        <template slot="prepend">
                            <span class="el-icon-user"></span>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" show-password placeholder="密码">
                        <template slot="prepend" maxlength="16">
                            <span class="el-icon-lock"></span>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('form')">登录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-checkbox v-model="form.checked">保持登录</el-checkbox>
                </el-form-item>
            </el-form>
        </div>
        <img src="@/assets/login/logo1.png" alt="" class="logo">
    </div>
</template>

<script>
    import {
        doLogin
    } from '@/api/loginController'
    import {
        factoryMess
    } from '@/api/userController'
    export default {
        data() {
            var account = (rule, value, callback) => {
                if (this.currentSpan == 0) {
                    if (value && !Number.isInteger(Number(value))) {
                        return callback(new Error('请输入合法的手机号'));
                    } else if (!value) {
                        return callback(new Error('请输入手机号'));
                    } else {
                        callback()
                    }
                } else if (this.currentSpan == 1) {
                    if (!value) {
                        return callback(new Error('请输入账号'));
                    } else {
                        callback()
                    }
                }
            };
            return {
                currentSpan: 0,
                form: {
                    type: 'mobile',
                    factoryCode: '',
                    account: '',
                    password: '',
                    checked: true
                },
                loading: false,
                rules: {
                    factoryCode: [{
                        required: true,
                        message: '请输入工厂代码',
                        trigger: 'blur'
                    }],
                    account: [{
                        required: true,
                        validator: account,
                        trigger: 'blur'
                    }, ],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }],
                }
            }
        },
        created() {
            document.addEventListener('keydown', (e) => {
                if (e.keyCode == 13) this.submitForm('form')
            })
        },
        methods: {
            //登录
            getDoLogin(data) {
                this.loading = true
                doLogin(data).then(res => {
                    if (res.code == 200) {
                        localStorage.setItem('token', res.data.token)
                        this.getFactoryMess()
                        this.$store.commit('setPermissionList', JSON.parse(res.data.permissions))
                        this.$store.commit('setAccount', res.data.account)
                        this.$router.push('/home')
                    }
                    this.loading = false
                })
            },
            //工厂信息
            getFactoryMess(data) {
                factoryMess(data).then(res => {
                    if (res.code == 200) {
                        this.$store.commit('setFactoryInfo', res.data)
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.form.type = this.currentSpan == 0 ? 'mobile' : 'factory'
                        this.getDoLogin(this.form)
                    } else {
                        return false;
                    }
                });
            },

            resetForm(formName) {
                this.$refs[formName].resetFields();
            },

            //切换登陆方式
            changeLoginWay(val) {
                this.currentSpan = val
                this.resetForm('form')
            }
        },
        beforeDestroy() {
            document.removeEventListener('keydown', (e) => {
                if (e.keyCode == 13) this.submitForm('form')
            })
        }
    }
</script>

<style lang="less" scoped>
    .login {
        position: relative;
        min-width: 1280px;
        height: 100%;
        background: url('~@/assets/login/BG.jpg') no-repeat;
        background-size: cover;

        .login_form {
            position: absolute;
            left: 50%;
            top: 50%;
            width: 380px;
            transform: translate(-50%, -50%);
            background: hsla(0, 0%, 100%, .95);
            border-radius: 30px;
            padding: 20px 40px;

            .title {
                height: 100px;
                font-size: 24px;
                font-weight: 700;
                color: #000;
                text-align: center;
                line-height: 100px;
            }

            .login_way {
                padding: 10px 20px 20px;
                font-size: 14px;

                span {
                    cursor: pointer;
                    color: rgba(0, 20, 14, .2);
                }

                .active {
                    color: rgb(0, 20, 14);
                }

                span:nth-child(1) {
                    float: left;
                }

                span:nth-child(2) {
                    float: right;
                }
            }

            /deep/ .el-form-item__error {
                padding: 4px;
            }

            /deep/ .el-form-item--mini.el-form-item {
                margin-bottom: 25px;
            }

            /deep/ .el-form-item--mini.el-form-item:last-child {
                margin-bottom: 0;
            }

            /deep/ .el-button--mini {
                width: 100%;
            }
        }

        .logo {
            width: 180px;
            margin: 11px 0 0 30px;
        }
    }
</style>